//
//  DayNightView.m
//  JW_BasePorject
//
//  Created by JW on 2018/7/4.
//  Copyright © 2018年 JW. All rights reserved.
//

#import "DayNightView.h"

@interface DayNightView ()

@property (copy, nonatomic) clickBlock clickBlock;


@property (nonatomic,weak) UIButton *selectedBtn;

@end

@implementation DayNightView

- (instancetype)initWithClickAction:(clickBlock)clickBlock{
    
    self = [super initWithFrame:CGRectMake(0, 50, SCREEN_WIDTH, 50)];
    
    if (self) {
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 70, 30)];
        titleLabel.text = @"模式选择";
        [self addSubview:titleLabel];
        
        UIButton *nightBtn = [self creatButtonWithFrame:CGRectMake(SCREEN_WIDTH - 70, 10, 60, 30) title:@"夜间" image:@"Paper_night_n" selectedImage:@"Paper_night_y"];
        [self addSubview:nightBtn];
        
        UIButton *dayBtn = [self creatButtonWithFrame:CGRectMake(CGRectGetMinX(nightBtn.frame)-100, 10, 70, 30) title:@"日间" image:@"Paper_Morning_n" selectedImage:@"Paper_Morning_y"];
        
        [self addSubview:dayBtn];
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(10, 49.5, SCREEN_WIDTH - 20, 0.5)];
        line.backgroundColor = [UIColor lightGrayColor];
        [self addSubview:line];
        
        self.clickBlock = clickBlock;
        
    }
    return self;    
}


- (void)btn_click:(UIButton *)sender{
    
    _selectedBtn.selected = NO;
    sender.selected = YES;
    _selectedBtn = sender;
    
    if (self.clickBlock) {
        self.clickBlock(sender);
    }
    
}

- (UIButton *)creatButtonWithFrame:(CGRect)frame title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectImage{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:selectImage] forState:UIControlStateSelected];
    [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [btn setTitleColor:ZhuTiColor forState:UIControlStateSelected];
    
    [btn addTarget:self action:@selector(btn_click:) forControlEvents:UIControlEventTouchUpInside];

    return btn;
}


@end
